<!-- 袋皮库存列表 -->
<template>
  <div>
    <div
      class="bag_inventory_list"
      v-if="!appStore.isPhone">
<!--      <SearchCard-->
<!--          ref="searchCardRef"-->
<!--          :searchTemplate="searchTemplate"-->
<!--          :more="true"-->
<!--          @add-event="addEvent"-->
<!--          @reset="getList"-->
<!--          @search="(data) => getList(data)" />-->
      <el-card
        class="search_card"
        ref="searchCardRef">
        <el-form
          :model="formData"
          class="search_form">
          <el-row>
            <el-form-item label="米厂">
              <el-select
                v-model="formData.baseRiceBarnId"
                placeholder="米厂"
                style="width: 130px"
                filterable
                clearable>
                <el-option
                  v-for="item in riceFactorySimpleNameList"
                  :key="item.id"
                  :label="item.simple_name"
                  :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="袋皮">
              <el-select
                v-model="formData.bagInfoId"
                style="width: 130px"
                v-loading="bagInfoFactoryLoading"
                placeholder="袋皮简称"
                filterable
                clearable>
                <el-option
                  v-for="item in bagInfoSimpleNameList"
                  :key="item.id"
                  :label="item.simple_name"
                  :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="剩余袋数排序">
              <el-select
                v-model="formData.isAsc"
                style="width: 130px"
                placeholder="剩余袋数排序">
                <el-option
                  label="升序"
                  :value="true" />
                <el-option
                  label="降序"
                  :value="false" />
              </el-select>
            </el-form-item>
            <el-button
              type="primary"
              @click="getList(formData)"
              class="query_button">
              <template #icon>
                <el-icon>
                  <search />
                </el-icon>
              </template>
              {{ config.button.text.query }}
            </el-button>
            <el-button @click="reset">
              <template #icon>
                <el-icon>
                  <Refresh />
                </el-icon>
              </template>
              {{ config.button.text.reset }}
            </el-button>
          </el-row>
        </el-form>
      </el-card>
      <el-card class="table_card">
        <Table
          :data="tableData"
          v-loading="tableLoading"
          :height="height">
          <el-table-column
            prop="baseRiceBarnFullName"
            label="米厂全称"
            min-width="180"
            :resizable="false"
          />
          <el-table-column
            prop="baseRiceBarnSimpleName"
            label="米厂简称"
            min-width="120"
            :resizable="false"
          />
          <el-table-column
            prop="baseRiceBarnGoodsFullName"
            label="商品全称"
            min-width="120"
            :resizable="false" />
          <el-table-column
            prop="baseRiceBarnGoodsSimpleName"
            label="商品简称"
            min-width="120"
            :resizable="false" />
          <el-table-column
            prop="bagInfoFullName"
            label="袋皮全称"
            min-width="180"
            :resizable="false" />
          <el-table-column
            prop="bagInfoSimpleName"
            label="袋皮简称"
            min-width="120"
            :resizable="false" />
          <el-table-column
            prop="count"
            min-width="80"
            label="剩余袋数"
            :resizable="false" />
        </Table>
        <Pagination
          :pageNum="pageNum"
          :pageSize="pageSize"
          :total="total"
          @handle-size-change="(val) => pageSize = val"
          @handle-current-change="(val) => pageNum = val" />
      </el-card>
      <!-- 查看 -->
    </div>
    <!-- 手机端页面 -->
    <h5Index v-else></h5Index>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import Pagination from "@/components/Pagination.vue"
import Table from "@/components/Table.vue"
import ApiBagInventory from "@/api/modules/bag_inventory"
import { useTable } from "@/hooks/useTable"
import h5Index from "./h5Index.vue";
import config from "@/common/element-plus/config";
import app from "@/common/config/app";
import { useAppStore } from "@/stores/app"
import { storeToRefs } from "pinia";
import SearchCard from "@/components/SearchCard.vue"


// 查询表单
const formData = ref({isAsc: false})
// 表格高度(固定表头)
const height = ref(0)
// 查询卡片实例
const searchCardRef = ref()
const riceFactorySimpleNameList = ref([])
const bagInfoSimpleNameList = ref([])

// 查询模板
const searchTemplate = ref([
  {
    label: "资金仓库名称",
    type: "simpleSelect",
    placeholder: "资金仓库名称",
    interfaceUrl: "baseCapitalWarehouse",
    value: "baseCapitalWarehouseId",
    isClosed: false,
  },
  {
    label: "商品名称",
    type: "input",
    placeholder: "请输入商品名称",
    value: "goodsName",
    isClosed: false,
  },
  {
    label: "是否完成提货",
    type: "yesNo",
    placeholder: "选择",
    value: "isFinishPick",
    isClosed: false,
  },
  {
    label: "车牌号",
    type: "input",
    placeholder: "请输入车牌号",
    value: "carNumber",
    isClosed: true,
  },
  {
    label: "司机名称",
    type: "input",
    placeholder: "请输入司机名称",
    value: "driverName",
    isClosed: true,
  },
  {
    label: "创建日期",
    type: "daterange",
    value: "date",
    isClosed: true,
  },
]);

const { tableMethods, tableState, pagination } = useTable({
  fetchDataApi: async (data) => {
    if (!data){
      data={isAsc: false}
    }
    const { pageSize, pageNum } = pagination
    const res = await ApiBagInventory.selectListPage(data, {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    })
    return {
      list: res.data.records,
      total: res.data.total
    }
  }
})
const { getList } = tableMethods
const { pageSize, pageNum } = pagination
const { total, tableLoading, tableData } = tableState

// 重置
const reset = () => {
  formData.value = {isAsc: false}
  getList()
}


// 监听查询卡片大小变化
const resizeObserver = new ResizeObserver(entries => {
  for (const entry of entries) {
    if (contentFullScreen.value) {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - entry.contentRect.height) + "px"
    } else {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - app.headerHeight - entry.contentRect.height) + "px"
    }
  }
})

//获取米厂简称列表
const getRiceFactorySimpleNameList = async () => {
  const data = await ApiBagInventory.getSimpleNameList("/baseRiceBarn/")
  riceFactorySimpleNameList.value = data.data
}

//获取袋皮简称列表
const getBagInfoSimpleNameList = async () => {
  const data = await ApiBagInventory.getSimpleNameList("/bagInfo/")
  bagInfoSimpleNameList.value = data.data
}


const appStore = useAppStore()

// 保持响应式
const { contentFullScreen } = storeToRefs(appStore)

onMounted(() => {
  if (appStore.isPhone) return
  resizeObserver.observe(searchCardRef.value.$el)
  getRiceFactorySimpleNameList()
  getBagInfoSimpleNameList()
})
</script>

<style lang="scss" scoped>
.bag_inventory_list {
  .search_card {
    margin-bottom: 4px;

    :deep(.el-card__body) {
      padding: 10px;
      display: flex;
      justify-content: space-between;
    }

    .el-button {
      margin: 5px 10px 5px 0;
    }

    .query_button {
      margin-left: 100px;
    }

    .el-form-item {
      margin-right: 10px;
      padding: 5px 0;
    }
  }

  .table_card {
    :deep(.el-card__body) {
      padding: 0;
    }
  }
}
</style>
